import 'package:flutter/material.dart';
import 'package:flutter_default_project/home/todo_list_view.dart';
import 'package:flutter_default_project/stat_view.dart';
import 'package:flutter_default_project/todo_list_model.dart';
import 'package:provider/provider.dart';
import 'package:todos_app_core/todos_app_core.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

enum _HomeScreenTab { todos, stats }

class _HomeScreenState extends State<HomeScreen> {
  final _tab = ValueNotifier(_HomeScreenTab.todos);
  @override
  void dispose() {
    _tab.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Screen'),
        actions: [
          PopupMenuButton(
            onSelected: (value) {
              Provider.of<TodoListModel>(context, listen: false).filter = value;
            },
            itemBuilder: (context) {
              return [
                const PopupMenuItem(
                  child: Text('all'),
                  value: VisibilityFilter.all,
                ),
                const PopupMenuItem(
                  child: Text('active'),
                  value: VisibilityFilter.active,
                ),
                const PopupMenuItem(
                  child: Text('completed'),
                  value: VisibilityFilter.completed,
                ),
              ];
            },
          ),
          PopupMenuButton(
            itemBuilder: (context) {
              return [
                PopupMenuItem(
                  child: Text('Clear Completed'),
                  onTap: () {
                    Provider.of<TodoListModel>(
                      context,
                      listen: false,
                    ).clearCompleted();
                  },
                ),
                PopupMenuItem(
                  child: Text('toggle All'),
                  onTap: () {
                    Provider.of<TodoListModel>(
                      context,
                      listen: false,
                    ).toggleAll();
                  },
                ),
              ];
            },
          ),
        ],
      ),
      body: Selector<TodoListModel, bool>(
        builder: (_, isLoading, __) {
          if (isLoading) {
            return Center(child: CircularProgressIndicator());
          }
          return ValueListenableBuilder<_HomeScreenTab>(
            valueListenable: _tab,
            builder: (context, tab, _) {
              switch (tab) {
                case _HomeScreenTab.stats:
                  return StatView();
                case _HomeScreenTab.todos:
                default:
                  return TodoListView();
              }
            },
          );
          // return isLoading
          //     ? Center(child: CircularProgressIndicator())
          //     : TodoListView();
        },
        selector: (_, model) => model.isLoading,
      ),
      bottomNavigationBar: ValueListenableBuilder(
        valueListenable: _tab,
        builder: (context, tab, _) {
          return BottomNavigationBar(
            onTap: (value) {
              _tab.value = _HomeScreenTab.values[value];
            },
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.list), label: 'todos'),
              BottomNavigationBarItem(
                icon: Icon(Icons.show_chart),
                label: 'stats',
              ),
            ],
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pushNamed(context, ArchSampleRoutes.addTodo);
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
